package com.jic.product.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.entity.ProductDistribute;
import com.jic.product.manage.response.ProductDescribeResponse;
import com.jic.product.manage.response.ProductDistributeResponse;
import com.jic.product.mapper.ProductDistributeMapper;
import com.jic.product.mapper.ProductMapper;
import com.jic.product.service.ProductDistributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品分发记录表
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-15 05:53:49
 */
@Service
public class ProductDistributeServiceImpl implements ProductDistributeService {

    @Autowired
    private ProductDistributeMapper productDistributeMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public long insert(ProductDistribute productDistribute) {
        long flag = productDistributeMapper.insert(productDistribute);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return productDistributeMapper.delete(id);
    }

    @Override
    public long update(ProductDistribute productDistribute) {
        return productDistributeMapper.update(productDistribute);
    }

    @Override
    public ProductDistribute queryByPrimaryKey(Long id) {
        return productDistributeMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Long> queryByProductId(Long productId) {
        return productDistributeMapper.queryByProductId(productId);
    }

    @Override
    public RestResult<List<ProductDistributeResponse>> queryStoreIdByProductId(Long productId) {
        if (null == productId) {
            return RestResult.error("9999", "商品id不能为空");
        }
        List<ProductDistributeResponse> productDistributeResponses = productMapper.queryProductDistributeById(productId);
        return RestResult.success(productDistributeResponses);
    }

    @Override
    public PageResult<ProductDistribute> queryPage(ProductDistribute productDistribute, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductDistribute> list = productDistributeMapper.queryPage(startIndex, pageQuery.getLimit(), productDistribute);
        long count = productDistributeMapper.queryPageCount(productDistribute);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}
